package com.demo2do.boyle.backend.service.advertisement.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.advertisement.TaskService;
import com.demo2do.boyle.common.entity.aduser.order.PayType;
import com.demo2do.boyle.common.entity.customer.wallet.RecordType;
import com.demo2do.boyle.common.entity.customer.wallet.Wallet;
import com.demo2do.boyle.common.entity.customer.wallet.WalletRecord;
import com.demo2do.boyle.common.entity.task.Task;
import com.demo2do.boyle.common.entity.task.TaskStatus;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author leo
 */
@Service("customerTasksService")
public class TaskServiceImpl extends GenericServiceImpl<Task> implements TaskService {
	
	@Autowired 
	private GenericDaoSupport genericDaoSupport;
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.CustomerTasksService#count()
	 */
	public int count() {
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Task");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.CustomerTasksService#count(com.demo2do.boyle.common.entity.task.TaskStatus)
	 */
	public int count(TaskStatus taskStatus) {
		Map<String, Object> parameters = new HashMap<String, Object>(1);
		parameters.put("taskStatus", taskStatus);
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Task WHERE taskStatus = :taskStatus", parameters);
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.CustomerTasksService#count(com.demo2do.core.persistence.support.Filter)
	 */
	public int count(Filter filter) {
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Task " + filter.getWhereSentence(), filter.getParameters());
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.CustomerTasksService#list(com.demo2do.core.web.resolver.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<Task> list(Page page) {
		List<Task> tasks = genericDaoSupport.searchForList("FROM Task", page.getBeginIndex(), page.getMaxResultRecords());
		page.calculate(tasks.size());
		return tasks.size() > page.getEveryPage() ? tasks.subList(0, page.getEveryPage()) : tasks;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.CustomerTasksService#list(com.demo2do.core.web.resolver.Page, com.demo2do.boyle.common.entity.task.TaskStatus)
	 */
	@SuppressWarnings("unchecked")
	public List<Task> list(TaskStatus taskStatus, Page page) {
		
		if(taskStatus == null)
			taskStatus = TaskStatus.CLAIMED;
		
		List<Task> tasks = genericDaoSupport.searchForList("FROM Task WHERE taskStatus = :taskStatus", "taskStatus",  taskStatus, page.getBeginIndex(), page.getMaxResultRecords() );
		page.calculate(tasks.size());
		return tasks.size() > page.getEveryPage() ? tasks.subList(0, page.getEveryPage()) : tasks;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.advertisement.TaskService#audit(com.demo2do.boyle.common.entity.task.Task)
	 */
	public void auditPass(Task task) {
		//1.update task 
		genericDaoSupport.update(task);
		
		//2.update customer wallet
		Wallet wallet = genericDaoSupport.load(Wallet.class, task.getCustomer().getId());
		genericDaoSupport.update(wallet.audit(task.getReward()));
		
		//3.create wallet record
		WalletRecord walletRecord = new WalletRecord(wallet, RecordType.TASK_REWARD, PayType.ALIPAY);
		genericDaoSupport.save(walletRecord.initialize(task.getReward(), task.getId()));
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.advertisement.TaskService#audit(com.demo2do.boyle.common.entity.task.Task)
	 */
	public void auditUnpass(Task task) {
		//1.update task 
		genericDaoSupport.update(task);
	}

	/*@Override
	public int countFinishedNum(Advertisement advertisement) {
		
		//'select count(*) from advertisement_customer where status=3';
		Map<String, Object> parameters = new HashMap<String, Object>(1);
		parameters.put("taskStatus", TaskStatus.FINISH);
		parameters.put("advertisement", advertisement);
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM Task WHERE taskStatus = :taskStatus and advertisement = :advertisement", parameters);
		
	}*/

	@Override
	public void updateOtherStatus(Task task) {
		
		if (task.getTaskStatus() != TaskStatus.FINISH) {
			task.setTaskStatus(TaskStatus.OVERDUE);
		}
		genericDaoSupport.update(task);
	}

}
